package textapp.dao.model;

import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@NamedQueries({
	@NamedQuery(name="Message.getByUserIdAndTime", query="select m from Message m where m.to = :toUser and m.createDate > :beginTime"),
	@NamedQuery(name="Message.getConversationByTime", query="select m from Message m where m.to = :toUser and m.from = :fromUser and m.createDate > :beginTime")
})

@Entity
@Table(name="message")
public class Message {

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "mid", nullable = false)
	private int id;
	
	@ManyToOne
	@JoinColumn(name="to_uid", nullable=false)
	private User to;
	
	@ManyToOne
	@JoinColumn(name="from_uid", nullable=false)	
	private User from;
	
	@Column(name="text", nullable=true)
	private String text;
	
	@Column(name="type")
	private int type; // 0 = normal, 1 = notification
	
	@Column(name="status")
	private int status; // 0 = saved by server, 1 = saved by client
	
	@Basic(optional = false)
    @Column(name = "create_date", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
	private Date createDate;
	
	@Basic(optional = false)
    @Column(name = "modify_date", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
	private Date modifyDate;
	
	@Column(name="lifetime")
	private int lifetimeMin; // minutes 

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public User getTo() {
		return to;
	}

	public void setTo(User to) {
		this.to = to;
	}

	public User getFrom() {
		return from;
	}

	public void setFrom(User from) {
		this.from = from;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getModifyDate() {
		return modifyDate;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}

	public int getLifetimeMin() {
		return lifetimeMin;
	}

	public void setLifetimeMin(int lifetimeMin) {
		this.lifetimeMin = lifetimeMin;
	}
	
	
	
}
